/**
 * String.prototype.namespace()
 * http://remysharp.com/2008/02/04/javascript-namespaces/
 *
 * Creates an object namespace chain delimited by the $separator.
 * 
 * @param string $separator
 * @return void
 */
String.prototype.namespace = function(separator) {
    var ns = this.split(separator || '.'), p = window;
    for (i = 0; i < ns.length; i++) {
        p = p[ns[i]] = p[ns[i]] || {};
    }
};

'blogs.ajax.callbacks'.namespace();
'blogs.comments'.namespace();

var $b = blogs;

$b.ajax.comments = function(form, post_id) {
    var data ={
        'name': form.name.value,
        'email': form.email.value,
        'message': form.message.value 
    };
    
    $("#commentForm input:submit")[0].disabled = true;
    var table = $("#commentForm table");
    var offset = table.offset();
    var height = table.height();
    var width = table.width();
    
    $("#commentForm .loading").height(height).width(width).css({
        top: offset.top,
        left: offset.left
    }).fadeTo(1, 0.7);
    
    $.ajax({
        type: "POST",
        url: $b.ajaxBaseURL+"comments/"+post_id+"/",
        data: $("#commentForm").serialize(),
        success: function(html) {
            $("#comments").html(html);
        }
    }); // $.ajax
    
    return false;
}; // $b.ajax.comments

$b.comments.reply = function(i) {
    $("#id_message").text("@"+i+": ");
    
    var targetOffset = $("#commentForm").offset().top;
    $("html,body").animate({scrollTop: targetOffset}, 1000);
    
    $("#id_name").focus();

    return false;
}; // $b.comments.reply

$b.comments.init_replies = function() {
    $("#comments div.comment a.reply").each(function(i, s) {
        $(this).click(function() {
            var target_id = this.href.split("#")[1];
            var target =  $("#"+target_id);
            var parent_div = $($(this).parents()[1]);
            var parent_id = parent_div.attr("id");
            var new_link = $(document.createElement("a"));
            
            new_link.attr("href", "#"+parent_id).html("back to "+$(parent_div.children("div.meta a")[0]).text().split(" - ")[0].toLowerCase()).attr(
                "rel", parent_id
            ).click(function() {
                $(this).remove();
               
                var target_id = this.href.split("#")[1];
                var target =  $("#"+target_id);
                var targetOffset = target.offset().top;
                $("html").animate({scrollTop: targetOffset}, 1000);
                $("body").animate({scrollTop: targetOffset}, 1000, function() {
                    $(target).effect("highlight", {color:"#F7F1B1"}, 2000);
                    $($(target).children(".content")).effect("highlight", {color:"#F7F1B1"}, 2000);
                });

                return false;
            }).css({
                "background-color": "black",
                color: "white",
                padding: "2px",
                "margin-right": "3px"
            }).attr("title", $(parent_div.children("div.content")[0]).text().substring(0,50)+"...");
            
            $(target.children("div.meta")).after(new_link);
            
            var targetOffset = target.offset().top;
            $("html").animate({scrollTop: targetOffset}, 1000);
            $("body").animate({scrollTop: targetOffset}, 1000, function() {
                $(target).effect("highlight", {color:"#F7F1B1"}, 2000);
                $($(target).children(".content")).effect("highlight", {color:"#F7F1B1"}, 2000);
            });

           
            return false;
        }); // click
    }); // each
}; // $b.comments.init_replies

$b.init = function() {
    //$b.comments.init_replies();
}; // $b.blogs.init
